#include <QObject>
#include <QQmlEngine>
#include <QNetworkReply>
#include <QProcess>
#include "sdk/public/steam/steam_api.h"

#ifndef STEAMAPIBACKEND_H
#define STEAMAPIBACKEND_H

class SteamAPIBackend : public QObject
{
    Q_OBJECT
    QML_ELEMENT
    QML_SINGLETON
public:
    Q_INVOKABLE bool Init();
    Q_INVOKABLE QString GetPersonalName();
    Q_INVOKABLE QString GetPersonalAvatar();
    Q_INVOKABLE bool ConnectToGameServer();
    Q_INVOKABLE void RunCallbacks();
    Q_INVOKABLE void StartMatchMaking();
    Q_INVOKABLE void ManualHostGameSuccess();
    Q_INVOKABLE void TestLaunchGame();
    Q_INVOKABLE void RefreshPlayerScore();
    Q_INVOKABLE void RefreshQueueCount();
    Q_INVOKABLE void CancelMatchMaking();
    Q_INVOKABLE void CancelLaunchOrGame();
    Q_INVOKABLE void RefreshGameJoinState();
    Q_INVOKABLE void SendInvitation(bool invitationSent, QString sid);
    Q_INVOKABLE void SetTestLaunchComplete();
    Q_INVOKABLE void AcceptInvite(QString inviteSid);
    Q_INVOKABLE void DeclineInvite(QString inviteSid);
    Q_INVOKABLE void LeaveLobby();
    Q_INVOKABLE void LobbyKickPlayer(QString sid);
    Q_INVOKABLE void SetLobbyReady(bool ready);
    Q_INVOKABLE void RefreshGameHistory();

    QString token;
    QNetworkAccessManager *manager = NULL;
    int queueState = 0; // 0 not queue; 1 queuing; 2 match found, starting game; 3 wait host; 4 start host; -1 start game fail
    long lastCheckGameHostedTime = 0;
    long lastGameProcessCheckTime = 0;
    long lastFetchFriendsTime = 0;
    long lastFetchLobbyTime = 0;
    bool testLaunchComplete = false;
    QString gid;
    QProcess *launchGameProcess = NULL;
    int last_invite_id = 0;
    QString lobby_id;
public slots:
    void NAMReplyFinished(QNetworkReply *reply);
private:
    STEAM_CALLBACK(SteamAPIBackend, OnReceiveTicketForWebApi, GetTicketForWebApiResponse_t);
    STEAM_CALLBACK(SteamAPIBackend, OnReceiveRichPresenceJoinRequested, GameRichPresenceJoinRequested_t);
    long lastPlayerQueueTime;
    int is_host;
};

#endif // STEAMAPIBACKEND_H
